from selenium import webdriver
from time import sleep,time
from selenium.webdriver.support.select import Select
import random

from util import BoxDriver,BasePage
from login_page import LoginPage


class AddUserPage(LoginPage):

    # def __init__(self,driver):
    #     super().__init__(driver)

    def adduser(self,username='admin',password='123456'):
        driver = self.driver

        # 点击后台管理
        driver.click('id,s-menu-superadmin')

        driver.switch_to_frame('id,iframe-superadmin')

        for i in range(38,40):
            # 点击 添加成员
            driver.click('p,添加成员')

            # 用户名
            driver.input('id,account','user%d'%i)

            # 真实姓名
            driver.input('id,realname','user%d'%i)

            # 性别
            driver.click('id,genderm' if i%2==0  else 'id,genderf')

            # 部门
            driver.select_by_index('id,dept',random.randint(1,6))
            # 角色
            driver.select_by_index('id,role',random.randint(1,16))

            # 密码
            driver.input('id,password1','123456')

            # 密码
            driver.input('id,password2','123456')

            # 邮箱
            driver.input('id,email','user%d@163.com'%i)

            # 保存
            driver.click('id,submit')
            # 针对 NoSuchWindow问题
            sleep(2)

            # 跳转到最后一页
            driver.input('id,_pageID','10000')
            driver.click('id,goto')
            sleep(2)

            # 获取最后一行的真实姓名
            realname = driver.locate_elements('x,/html/body/div/div/div/div[2]/div/div/table/tbody/tr/td[2]')[-1].text
            # 断言
            assert realname == 'user%d'%i,'添加用户用例失败！'

        driver.close()

if __name__ == "__main__":
    driver = BoxDriver()
    add = AddUserPage(driver)
    add.login()
    add.adduser()